// See the Electron documentation for details on how to use preload scripts:
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts
const { ipcRenderer,contextBridge } = require('electron/renderer')

window.addEventListener('DOMContentLoaded', () => {

    const el = document.getElementById('other')
    el.addEventListener('mouseenter', () => {
        ipcRenderer.send('set-ignore-mouse-events', true,{ forward: true })
    })
    el.addEventListener('mouseleave', () => {
        ipcRenderer.send('set-ignore-mouse-events', false)
    })




    const codeButton = document.getElementById('setting')
    codeButton.addEventListener('click', () => {
        ipcRenderer.send('clickCodeButton', true)
    })
})

contextBridge.exposeInMainWorld('codePageAPI', {
    setHidden: (hidden) => ipcRenderer.send('setCodeHidden', hidden),
})
contextBridge.exposeInMainWorld('infoPageAPI', {
    setHidden: (hidden) => ipcRenderer.send('setInfoHidden', hidden),
})

contextBridge.exposeInMainWorld('AutoAPI', {
    keydown: (key) => ipcRenderer.send('keydown',key),
    screenshot: (args) => ipcRenderer.send('screenshot',args),
    click: (value) => ipcRenderer.send('click',value),
    dclick: (value) => ipcRenderer.send('dclick',value),
    move: (x,y) => ipcRenderer.send('move',x,y),
    drag: (x1,y1,x2,y2) => ipcRenderer.send('drag',x1,y1,x2,y2),
    keyTap: (key) => ipcRenderer.send('keytap',key),
    keyUp: (key) => ipcRenderer.send('keyup',key),
    cv: (args) => ipcRenderer.send('cv',args),
})

contextBridge.exposeInMainWorld('taskLogAPI', {
    sendTask: (info) => ipcRenderer.send('sendTask', info),
    onTask:(callback) => ipcRenderer.on('task', (_event, value) => callback(value)),
})

contextBridge.exposeInMainWorld('codeAPI', {
    sendToAI:(callback) => ipcRenderer.on('sendToAI', (_event, value)=> callback(value)),
    sendCode:(data) => ipcRenderer.send('sendCode', data),
    onGetCode:(callback) => ipcRenderer.on('onGetCode', (_event, value)=> callback(value)),
    sendCodes:(data) =>  ipcRenderer.send('sendCodes', data),
    getCode:(data) =>  ipcRenderer.send('getCode', data),
})

